package com.colin.datamove.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 * 与spring配置的线程池区别使用，避免业务较多的情况下，造成线程池阻塞
 */
public class ThreadPool {
    private static final Logger logger = LoggerFactory.getLogger(ThreadPool.class);
    /**
     * 核心线程数
     */
    private static final int corSize = 20;
    /**
     * 最大线程数
     */
    private static final int maxSize = 20;
    /**
     * 活跃时间
     */
    private static final long keepLive = 10;
    /**
     * 阻塞队列，默认队列长度为最大
     */
    private static final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
    /**
     * 线程池执行器
     */
    private static final ThreadPoolExecutor executor;

    static{
        executor = new ThreadPoolExecutor(
                corSize,
                maxSize,
                keepLive,
                TimeUnit.SECONDS,
                queue,
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        logger.info("ThreadPool_executor_init_success,size:{}", corSize);
    }

    public static void putThread(Runnable runnable){
        Future<?> submit = executor.submit(runnable);
    }

}
